#!/usr/bin/perl

do '../web-lib.pl';

&init_config();

#accounts_user_list(&user_fullname_list, &user_disable_list)
#return @user_list
sub accounts_user_list()
{
	my @user_list = ();
	my $pt_user_fullname_list = $_[0];
	my $pt_user_disable_list = $_[1];
	my @local_user = ();
	my @line_array = ();
	my $i = 0;

	my $openerror = open(LOCALUSER, $config{"local_user_file"});

	if(!$openerror)
	{
		@user_list = ();
		return @user_list;
	}

	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		chomp($local_user[$i]);
		@line_array = split(":", $local_user[$i]);
		if($line_array[0])
		{
			@user_list[$i] = $line_array[0];
			$pt_user_fullname_list->{$line_array[0]} = $line_array[1];
			$pt_user_disable_list->{$line_array[0]} = $line_array[2];
		}
		$i++;
	}

	@user_list = sort @user_list;

	return @user_list;
}

#get_user_grouplist($username)
#return @grouplist
sub get_user_grouplist()
{
	my @grouplist_rt=();
	my @groups_sys=();
	my @groups_local=();
	my @groups_tmp=();
	my @line_array=();
	my $username=$_[0];
	my $line;
	my $grouploc;
	my $groupsys;
	my ($i, $j, @members, %lc_gp);

	open(LOCALGROUP, $config{"local_group_file"});
	@groups_local = <LOCALGROUP>;
	close(LOCALGROUP);
	foreach $line (@groups_local)
	{
		chomp($line);
		$lc_gp{$line} = "yes";
	}

	open(SYSGROUP, $config{"sys_group_file"});

	@groups_sys = <SYSGROUP>;
	close(SYSGROUP);
	foreach $line (@groups_sys)
	{
		chomp($line);
	}

	$i = 0;
	foreach $groupsys (@groups_sys)
	{
		@line_array = split(":", $groupsys);
		@members = split(",", $line_array[3]);
		for($j = 0; $j < @members; $j++)
		{
			if(($members[$j] eq $username) && ($lc_gp{$line_array[0]} eq "yes"))
			{
				$groups_tmp[$i] = $line_array[0];
				$i++;
				last;
			}
		}
	}

#	for($i = 0; $i < @groups_tmp)

#	$line = join(",", @groups_tmp);

#	foreach $group_loc (@groups_local)
#	{
#		if($line eq $group_loc)
#		{
#			@grouplist_rt = ($group_loc, @grouplist_rt);
#		}
#	}

	@grouplist_rt = ();

	for($i = 0; $i < @groups_tmp; $i++)
	{
		if($lc_gp{$groups_tmp[$i]} eq "yes")
		{
			push @grouplist_rt, $groups_tmp[$i];
		}
	}

	sort(@grouplist_rt);

	return @grouplist_rt;
}

#get groupid just as users group id
#return the groupid of users which group name is "users"
sub get_users_groupid()
{
	my @groups_sys = ();
	my $i;
	my @line;
	open(SYSGROUP, $config{"sys_group_file"});
	@groups_sys = <SYSGROUP>;
	close(SYSGROUP);

	$i = 0;
	while($i < @groups_sys)
	{
		chomp($groups_sys[$i]);
		@line = split(":", $groups_sys[$i]);
		if($line[0] eq "users")
		{
			return $line[2];
		}
		$i++;
	}
}

#get_new_userid(void)
#get a new user id and id is not same as other users
#return id;
sub get_new_userid()
{
	my $id;
	my $i;
	my $base_uid = 20001;
	my %used_userid;
	my $max = 0;
	my @line;
	my @user_sys = ();
	my @localidfile;

	@user_sys = `getent passwd 2>&1`;

#	open(SYSUSER, $config{'sys_user_file'});
#	@user_sys = <SYSUSER>;
#	close(SYSUSER);

	open(LOCALID, $config{'localidfile'});
	@localidfile = <LOCALID>;
	close(LOCALID);

	for($i = 0; $i < @localidfile; $i++)
	{
		$localidfile[$i] =~ s/#.*$//;
		chomp($localidfile[$i]);
		if($localidfile[$i] =~ /uid/i)
		{
			@line = split(/\s+/, $localidfile[$i]);
			if($line[1] > $base_uid)
			{
				$base_uid = $line[1] + 1;
			}
			last;
		}
	}

	$i = 0;
	#print "@user_sys";
	while($i < @user_sys)
	{
		chomp($user_sys[$i]);
		@line = split(":", $user_sys[$i]);
		$used_userid{"$line[2]"} = "1";
		$i++;
	}

	$id = $base_uid;
	while($used_userid{"$id"})
	{
		$id++;
	}
	return $id;
}

#find_uid_by_name($uname)
#return $uid   -1  --> not found
sub find_uid_by_name()
{
	my ($uid, @user_sys, $i, @line, $username);
	$username = $_[0];

	open(SYSUSER, $config{'sys_user_file'});
	@user_sys = <SYSUSER>;
	close(SYSUSER);

	$i = 0;
	while($i < @user_sys)
	{
		chomp($user_sys[$i]);
		@line = split(":", $user_sys[$i]);
		if($line[0] eq $username)
		{
			return $line[2];
			last;
		}
		$i++;
	}
	if($i >= @user_sys)
	{
		return "-1";
	}
}

#create_user(%detail)
sub create_user()
{
	my ($users_detail, $i, $rt_msg, @glist);
	my (@line, $pt, @localid);
	$users_detail = $_[0];

	#lockuserfile

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});
	&lock_file($config{"localidfile"});

	&protect_shadow();
	&foreign_call("useradmin", "create_user", $users_detail);
	&unprotect_shadow();


	$users_detail->{'passmode'} = "3";

	#create users in other mofules
	&foreign_call("useradmin", "other_modules", "useradmin_create_user", $users_detail);

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "add_user", $users_detail);   #add user in /etc/pureftpd.passwd

	@glist = ();
	foreach $i (@{$users_detail->{'glist'}})
	{
		push @glist, $i;
	}

	&add_user_into_group(\@glist, $users_detail->{'user'});

	open(LOCALUSER, ">>$config{'local_user_file'}");
	#push SYSGROUP, $users_detail->{"user"}."\n";
	print LOCALUSER $users_detail->{"user"}.":".$users_detail->{"real"}.":".$users_detail->{"disabled"}."\n";
	close(LOCALUSER);

	&set_localid($users_detail->{"uid"}, -1);

	&sync_http_group();

	&unlock_file($config{"localidfile"});
	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");

	#excute dir should be absulutly?
	if($users_detail->{"disabled"} eq "yes")
	{
		&disable_user($users_detail->{'user'});
	}
	elsif($users_detail->{"disabled"} eq "no")
	{
		&enable_user($users_detail->{'user'});
	}
}

#find_in_array(\@array, $str)
#return an integer num -1 -> notfind   rt_find  -> address in array
sub find_in_array()
{
	my ($lref, $str, $rt_find, $i, $line);
	$lref = $_[0];
	$str = $_[1];
	$rt_find = -1;

	$i = 0;

	while($i < @$lref)
	{
		$line = $lref->[$i];
		if($line eq $str)
		{
			$rt_find = $i;
			return $rt_find;
		}
		$i++;
	}
	return $rt_find;
}

#get_user_passwd($username)
#return shadow passwd
sub get_user_passwd()
{
	my ($rt_pass, @sys_shadow, $i, @line, $username);

	@sys_shadow = ();
	@line = ();
	$i = 0;
	$username = @_[0];
	$rt_pass = "";

	open(SYS_SHADOW, $config{"sys_shadow"});
	@sys_shadow = <SYS_SHADOW>;
	close(SYS_SHADOW);

	$i = 0;
	while($i < @sys_shadow)
	{
		@line = split(":", $sys_shadow[$i]);
		if($line[0] eq $username)
		{
			$rt_pass = $line[1];
			last;
		}
		$i++;
	}

	return $rt_pass;
}

#get_user_detail(&detail, $username)
#return uid -1 --> not found
sub get_user_detail()
{
	my ($uid, $shell, $pass, $num, $max, $change, $expire, $min, $real, $real, $gid, $inactive);
	my ($user, $home, $line, $sline, $warn);
	my ($i, $username, @sys_passwd, @sys_shadow, $pt_detail, @linearray);

	$pt_detail = @_[0];
	$username = @_[1];

	open(SYS_PASSWD, $config{"sys_user_file"});
	@sys_passwd = <SYS_PASSWD>;
	close(SYS_PASSWD);

	open(SYS_SHADOW, $config{"sys_shadow"});
	@sys_shadow = <SYS_SHADOW>;
	close(SYS_SHADOW);

	$i = 0;
	while($i < @sys_passwd)
	{
		chomp($sys_passwd[$i]);
		@linearray = split(":", $sys_passwd[$i]);
		if($linearray[0] eq $username)
		{
			$num = $i;
			$line = $i;
			last;
		}
		$i++;
	}

	if($i >= @sys_passwd)
	{
		return "-1";
	}

	$i = 0;
	while($i < @sys_shadow)
	{
		chomp($sys_shadow[$i]);
		@linearray = split(":", $sys_shadow[$i]);
		if($linearray[0] eq $username)
		{
			$sline = $i;
			last;
		}
		$i++;
	}


	@linearray = split(":", $sys_passwd[$line]);
	$uid = $linearray[2];
	$shell = $linearray[6];
	$home = $linearray[5];
	$real = $linearray[4];
	$gid = $linearray[3];
	$user = $linearray[0];

	@linearray = split(":", $sys_shadow[$line]);
	$pass = $linearray[1];
	$change = $linearray[2];
	$min = $linearray[3];
	$max = $linearray[4];
	$expire = "";
	$inactive = "";
	$warn = "";

	$pt_detail->{"shell"} = $shell;
	$pt_detail->{"pass"} = $pass;
	$pt_detail->{"num"} = $num;
	$pt_detail->{"max"} = $max;
	$pt_detail->{"change"} = $change;
	$pt_detail->{"expire"} = $expire;
	$pt_detail->{"min"} = $min;
	$pt_detail->{"real"} = $real;
	$pt_detail->{"gid"} = $gid;
	$pt_detail->{"inactive"} = $inactive;
	$pt_detail->{"user"} = $username;
	$pt_detail->{"line"} = $line;
	$pt_detail->{"home"} = $home;
	$pt_detail->{"sline"} = $sline;
	$pt_detail->{"warn"} = $warn;
	$pt_detail->{"uid"} = $uid;

	return $uid;
}

#modify_user(\%user, \%olduser)
sub modify_user()
{
	my ($pt_user_detail, $pt_olduser_detail);
	my (@sys_group, @sys_pssswd, @sys_shadow, @local_user, @local_group, @glist);
	my ($i, @line);

	$pt_user_detail = @_[0];
	$pt_olduser_detail = @_[1];

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});	
	
	&protect_shadow();
	&foreign_call("useradmin", "modify_user", $pt_olduser_detail, $pt_user_detail);
	&unprotect_shadow();


	&foreign_call("useradmin", "other_modules", "useradmin_modify_user", $pt_user_detail, $pt_olduser_detail);

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "mod_user", $pt_olduser_detail, $pt_user_detail);   #modify user in /etc/pureftpd.passwd

	&remove_user_from_group($pt_olduser_detail->{'user'});

	@glist = ();
	foreach $i (@{$pt_user_detail->{'glist'}})
	{
		push @glist, $i;
	}

	&add_user_into_group(\@glist, $pt_user_detail->{'user'});

	&modify_local_user($pt_olduser_detail, $pt_user_detail);


	&sync_http_group();

	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");

	if($pt_user_detail->{"disabled"} eq "yes")
	{
		&disable_user($pt_user_detail->{'user'});
	}
	elsif($pt_user_detail->{"disabled"} eq "no")
	{
		&enable_user($pt_user_detail->{'user'});
	}
}

#remove_user_from_group($username)
sub remove_user_from_group()
{
	my(@line, @lref_sys_file, @lineuser, @lref_gshadow);
	my $i, $j, $pt_glist, $username;

	$username = @_[0];

	open(SYSGROUP, $config{"sys_group_file"});
	@lref_sys_file = <SYSGROUP>;
	close(SYSGROUP);

	open(SYSGSHADOW, $config{"sys_gshadow_file"});
	@lref_gshadow = <SYSGSHADOW>;
	close(SYSGSHADOW);

	$i = 0;
	while($i < @lref_sys_file)
	{
		@line = split(":", $lref_sys_file[$i]);

		chomp @line[3];
		if($line[3])
		{
			@lineuser = split(",", $line[3]);
			$j = 0;
			while($j < @lineuser)
			{
				if($lineuser[$j] eq "$username")
				{
					splice(@lineuser, $j, 1);
					last;
				}
				$j++;
			}
			$line[3] = join(",", @lineuser);
			$lref_sys_file[$i] = join(":", @line);
			$lref_sys_file[$i] = $lref_sys_file[$i]."\n";
		}
		$i++;
	}

	$i = 0;
	while($i < @lref_gshadow)
	{
		@line = split(":", $lref_gshadow[$i]);

		chomp @line[3];
		if($line[3])
		{
			@lineuser = split(",", $line[3]);
			$j = 0;
			while($j < @lineuser)
			{
				if($lineuser[$j] eq "$username")
				{
					splice(@lineuser, $j, 1);
					last;
				}
				$j++;
			}
			$line[3] = join(",", @lineuser);
			$lref_gshadow[$i] = join(":", @line);
			$lref_gshadow[$i] = $lref_gshadow[$i]."\n";
		}
		$i++;
	}

	open(SYSGROUP, ">$config{'sys_group_file'}");
	print SYSGROUP @lref_sys_file;
	close(SYSGROUP);

	open(SYSGSHADOW, ">$config{'sys_gshadow_file'}");
	print SYSGSHADOW @lref_gshadow;
	close(SYSGSHADOW);
}

#add_user_from_group(\@glist, $username)
sub add_user_into_group()
{
	my(@line, @lref_sys_file, @lref_gshadow);
	my $i, $pt_glist, $username;

	$pt_glist = @_[0];
	$username = @_[1];

	open(SYSGROUP, $config{"sys_group_file"});
	@lref_sys_file = <SYSGROUP>;
	close(SYSGROUP);

	open(SYSGSHADOW, $config{"sys_gshadow_file"});
	@lref_gshadow = <SYSGSHADOW>;
	close(SYSGSHADOW);

	$i = 0;
	while($i < @lref_sys_file)
	{
		@line = split(":", $lref_sys_file[$i]);

		#print "<p>line --> ", @line;
		if(&find_in_array($pt_glist, $line[0]) >= 0)
		{
			my $tmparray;
			my @tmpusers;

			chomp($line[3]);
			#print "<p>line3 --> $line[3]";
			if($line[3])
			{
				$line[3] = $line[3].",".$username;
				#print "<p>defined", "  ", $line[3];
			}
			else
			{
				$line[3] = $username;
				#print "<p>not defined $line[3]";
			}
			$line[3] = $line[3]."\n";
			$lref_sys_file[$i] = join(":", @line);
			#print "<p>$lref_sys_file[$i]";
		}
		$i++;
	}

	$i = 0;
	while($i < @lref_gshadow)
	{
		@line = split(":", $lref_gshadow[$i]);

		#print "<p>line --> ", @line;
		if(&find_in_array($pt_glist, $line[0]) >= 0)
		{
			my $tmparray;
			my @tmpusers;

			chomp($line[3]);
			#print "<p>line3 --> $line[3]";
			if($line[3])
			{
				$line[3] = $line[3].",".$username;
				#print "<p>defined", "  ", $line[3];
			}
			else
			{
				$line[3] = $username;
				#print "<p>not defined $line[3]";
			}
			$line[3] = $line[3]."\n";
			$lref_gshadow[$i] = join(":", @line);
		}
		$i++;
	}


	open(SYSGROUP, ">$config{'sys_group_file'}");
	print SYSGROUP @lref_sys_file;
	close(SYSGROUP);

	open(SYSGSHADOW, ">$config{'sys_gshadow_file'}");
	print SYSGSHADOW @lref_gshadow;
	close(SYSGSHADOW);
}

#modify local user name to same as passwd file change
#modify_local_user($olduser, $newuser)
sub modify_local_user()
{
	my (@local_user, $i, @line, $olduser, $newuser);

	$olduser = @_[0];
	$newuser = @_[1];

	open(LOCALUSER, $config{"local_user_file"});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $olduser->{"user"})
		{
			$local_user[$i] = join(":", $newuser->{"user"}, $newuser->{"real"}, $newuser->{"disabled"});
			$local_user[$i] = $local_user[$i]."\n";
			last;
		}
		$i++;
	}

	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close(LOCALUSER);
}

#delete_user($username)
sub delete_user()
{
	my (%detail, $username);
	$username = @_[0];

	&enable_user($username);

	&get_user_detail(\%detail, $username);

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});

	#delete in other modules
	&foreign_call("useradmin", "other_modules", "useradmin_delete_user", \%detail);

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "del_user", $username);   #delete user in /etc/pureftpd.passwd

	#delete in passwd and shadow
	&protect_shadow();
	&foreign_call("useradmin", "delete_user", \%detail);
	&unprotect_shadow();


	&remove_user_from_group($username);

	&delete_local_user($username);

	&sync_http_group();

	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");
}

#when delete a localuser for unix in localfile will change as unix
#delete_local_user($username)
sub delete_local_user()
{
	my (@local_user, $i, @line, $username);

	$username = @_[0];


	open(LOCALUSER, $config{"local_user_file"});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = @local_user - 1;
	while($i >= 0)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			splice(@local_user, $i, 1);
			last;
		}
		$i--;
	}

	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close(LOCALUSER);
}

#disable_user($username)
sub disable_user()
{
	my ($username, @local_user, $i, @line);

	$username = @_[0];

	open(LOCALUSER, $config{'local_user_file'});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			chomp($line[2]);
			#system("/usr/bin/passwd -l $username 2>/dev/null");
			#my $out = &ez_system('/usr/bin/passwd', '-l', $username);
			
			&disable_shadow($username);

			$out = &ez_system('smbpasswd', '-d', $username);
			#system("smbpasswd -d $username 2>/dev/null");
			&disable_http_user($username);
			$line[2] = "yes"."\n";
			$local_user[$i] = join(":", @line);
			last;
		}
		$i++;
	}

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "disable_user", $username);   #disable user in /etc/pureftpd.passwd

	&lock_file($config{'local_user_file'});
	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close LOCALUSER;
	&unlock_file($config{'local_user_file'});
}

#enable_user($username)
sub enable_user()
{
	my($username, @local_user, $i, @line);

	$username = @_[0];

	open(LOCALUSER, $config{'local_user_file'});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			chomp($line[2]);
			#my $out = ez_system('/usr/bin/passwd', '-u', $username);
			&enable_shadow($username);
			$out = ez_system('smbpasswd', '-e', $username);

			&enable_http_user($username);
			$line[2] = "no"."\n";
			$local_user[$i] = join(":", @line);
			last;
		}
		$i++;
	}

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "enable_user", $username);   #enable user in /etc/pureftpd.passwd

	&lock_file($config{'local_user_file'});
	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close LOCALUSER;
	&unlock_file($config{'local_user_file'});
}

#get_local_group_list(\%groupmember)
#return @grouplist
sub get_local_group_list()
{
	my (@grouplist, $pt_groupmember, $i, @sysgroup, @localgroup, @line, $addr);

	@grouplist = ();

	open(SYSGROUP, "$config{'sys_group_file'}");
	@sysgroup = <SYSGROUP>;
	close(SYSGROUP);

	open(LOCALGROUP, "$config{'local_group_file'}");
	@localgroup = <LOCALGROUP>;
	close(LOCALGROUP);

	$i = 0;
	while($i < @localgroup)
	{
		chomp($localgroup[$i]);
		$i++;
	}

	$i = 0;
	while($i < @sysgroup)
	{
		@line = split(":", $sysgroup[$i]);
		$addr = &find_in_array(\@localgroup, $line[0]);
		if($addr >= 0)
		{
			chomp($line[3]);
			$pt_groupmember->{"$line[0]"} = $line[3];
			push @grouplist, $line[0];
		}
		$i++;
	}

	@grouplist = sort @grouplist;
	return @grouplist;
}

#local_user_exist($username)
sub local_user_exist()
{
	my ($i, @localuser, @line, $username);
	$username = @_[0];

	open(LOCALUSER, $config{"local_user_file"});
	@localuser = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @localuser)
	{
		chomp($localuser[$i]);
		@line = split(":", $localuser[$i]);
		if($line[0] eq $username)
		{
			return $i;
			last;
		}
		$i++;
	}
	if($i >= @localgroup)
	{
		return "-1";
	}
}

#sync_user_with_quota($username)
sub sync_user_with_quota()
{
	my ($i, @user_def_quota, $username);

	$username = @_[0];


	&foreign_require("quota", "quota-lib.pl");
	#&foreign_call("quota", "edit_user_quota", $username, $dir, $sblocks, $hblocks, $sfiles, $hfiles);

	open(USERQUOTA, "$config{'userdefaultquota'}");
	@user_def_quota = <USERQUOTA>;
	close(USERQUOTA);


	$i = 0;
	while($i < @user_def_quota)
	{
		$user_def_quota[$i] =~ s/#.*$//;
		chomp($user_def_quota[$i]);
		@line = split(/\s+/, $user_def_quota[$i]);
		if($line[0])
		{
			&foreign_call("quota", "edit_user_quota", $username, $line[0], $line[1], $line[2], $line[3], $line[4]);
		}
		$i++;
	}
}

#get_localid(void)
#return @localid;
sub get_localid()
{
	my @localid, $i;
	open(LOCALID, "$config{'localidfile'}");
	@localid = <LOCALID>;
	close(LOCALID);

#	for ($i = 0; $i < @localid; $i++)
#	{
#		chomp($localid[$i]);
#	}
	return @localid;
}

#$set_localid($uid, $gid)
#return void
sub set_localid()
{
	my @localid, $i, $uid, $gid;
	$uid = @_[0];
	$gid = @_[1];

	@localid = &get_localid();
	for ($i = 0; $i < @localid; $i++)
	{
		if(($localid[$i] =~ /uid/i) && ($uid > 0))
		{
			$localid[$i] = join(" ", "uid", $uid, "\n");
		}
		if(($localid[$i] =~ /gid/i) && ($gid > 0))
		{
			$localid[$i] = join(" ", "gid", $gid, "\n");
		}
	}

	open(LOCALID, ">$config{'localidfile'}");
	print LOCALID @localid;
	close(LOCALID);
}

#disable_http_user($username)
sub disable_http_user()
{
	my @httpwd, $i, @line, $username;

	$username = @_[0];

	open(HTTPWD, "$config{'httppasswd'}");
	@httpwd = <HTTPWD>;
	close(HTTPWD);

	$i = 0;
	for($i = 0; $i < @httpwd; $i++)
	{
		$httpwd[$i] =~ s/#.*$//;
		if($httpwd[$i])
		{
			@line = split(":", $httpwd[$i]);
			if($line[0] eq $username)
			{
				$line[1] = '!'.$line[1];
				$httpwd[$i] = join(":", @line);
				$httpwd[$i] = $httpwd[$i]."\n";
				last;
			}
		}
	}

	open(HTTPWD, ">$config{'httppasswd'}");
	print HTTPWD @httpwd;
	close(HTTPWD);
}

#enable_http_user($username)
sub enable_http_user()
{
	my @httpwd, $i, @line, $username;

	$username = @_[0];

	open(HTTPWD, "$config{'httppasswd'}");
	@httpwd = <HTTPWD>;
	close(HTTPWD);

	$i = 0;
	for($i = 0; $i < @httpwd; $i++)
	{
		$httpwd[$i] =~ s/#.*$//;
		if($httpwd[$i])
		{
			@line = split(":", $httpwd[$i]);
			
			if($line[0] eq $username)
			{
				$line[1] =~ s/^!+//;
				$httpwd[$i] = join(":", @line);
				$httpwd[$i] = $httpwd[$i]."\n";
				last;
			}
		}
	}

	open(HTTPWD, ">$config{'httppasswd'}");
	print HTTPWD @httpwd;
	close(HTTPWD);
}

#sub check_simbol_uname($inputname)
#return $error_msg
sub check_simbol_uname()
{
	my ($inputname, $error_msg);

	$inputname = @_[0];
	$error_msg = "";

	if(($inputname =~ /[^a-zA-Z0-9]/) || ($inputname =~ /^\d+(.*)/))
	{
		$error_msg = $text{'error_msg_isnt_simbol'};
	}

	return $error_msg;
}

sub sync_http_group()
{
	&foreign_require("groups", "accounts.pl");
	&foreign_call("groups", "sync_http_group");	
}

#sub check_simbol_fullname()
#return $error_msg
sub check_simbol_fullname()
{
	my ($inputname, $error_msg);

	$inputname = @_[0];
	$error_msg = "";

	unless($inputname)
	{
		return $error_msg;
	}

	if(($inputname =~ /[^a-zA-Z0-9\x7f-\xff\x20]/))
	{
		$error_msg = $text{'error_msg_isnt_fullname'};
	}

	return $error_msg;
}

#protect shadow file  copy shadow.bak to /tmp/ and touch shadow.tch on /tmp/
sub protect_shadow()
{
	my ($out);

	$out = &ez_system("cp", "-f", $config{"sys_shadow"}, $config{'shadowbak'});
	$out = &ez_system("touch", $config{'shadowtch'});
	
}

#remove shadow.tch protect file
sub unprotect_shadow()
{
	my ($out);

	if(-e $config{'shadowtch'})
	{
		$out = &ez_system("rm", "-f", $config{'shadowtch'});
	}
}

sub disable_shadow()
{
	my (@shadow, $i, $uname, @line);

	$uname = $_[0];

	open(SHADOW, $config{'sys_shadow'});

	@shadow = <SHADOW>;

	close(SHADOW);

	for($i = 0; $i < @shadow; $i++)
	{
	    $shadow[$i] =~ s/#.*$//;
	    
	    @line = split(/:/, $shadow[$i]);
	    if($line[0] eq $uname)
	    {
		unless($line[1] =~ /^!!/)
		{
		    $line[1] = '!!'.$line[1];
		    $shadow[$i] = join(":", @line);
		}
		last;
	    }
	}

	open(SHADOW, "> $config{'sys_shadow'}");
	print SHADOW @shadow;
	close(SHADOW);
}

sub enable_shadow()
{
	my (@shadow, $i, $uname, @line);

	$uname = $_[0];

	open(SHADOW, $config{'sys_shadow'});

	@shadow = <SHADOW>;

	close(SHADOW);

	for($i = 0; $i < @shadow; $i++)
	{
	    $shadow[$i] =~ s/#.*$//;
	    
	    @line = split(/:/, $shadow[$i]);
	    if($line[0] eq $uname)
	    {
		if($line[1] =~ /^!/)
		{
		    $line[1] =~ s/^!+//;
		    $shadow[$i] = join(":", @line);
		}
		last;
	    }
	}

	open(SHADOW, "> $config{'sys_shadow'}");
	print SHADOW @shadow;
	close(SHADOW);
}

#remote_foreigncall for mdm module
#mdm_adduser($username, $fullname, $passwd)
sub mdm_adduser()
{
        my ($username, $fullname, $passwd, $error_msg, $uid, @user, $newpasswd);

	$username = @_[0];
	$fullname = @_[1];
	$passwd = @_[2];
	$error_msg = "";

	$uid = &local_user_exist($username);
	unless ($uid < 0)
	{
	        $error_msg = $text{'error_msg_user_exist_local'};
		return $error_msg;
	}		

	$uid = &find_uid_by_name($username);
	unless ($uid < 0)
	{
	        $error_msg = $text{'error_msg_user_exist_sys'};
		return $error_msg;
	}

	$user{"min"} = 0;
	$user{"shell"} = "/sbin/nologin";
	$user{"real"} = $fullname;
	$user{"gid"} = &get_users_groupid();
	$user{"inactive"} = "";
	$user{"user"}= $username;
	$user{"home"}= "/home/abc";
	$user{"plainpass"}= $passwd;
		
	&foreign_require("setpass", "setpass-lib.pl");
	$newpasswd = &foreign_call("setpass", "crt_cryptpasswd", $passwd);
		
	$user{"pass"}=$newpasswd;
	$user{"max"}=9999;
	$user{"change"}=11915;
	$user{"warn"}="";
	$user{"expire"}="";
	$user{"uid"}= &get_new_userid();

	$user{"disabled"} = "no";
		
	@glist = split(/\s+/, "");
	$user{"glist"} = [@glist];
	

	&create_user(\%user);
	&sync_user_with_quota($username);
	
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_deluser($username)
sub mdm_deluser()
{
        my ($username, %user, $uid, $error_msg);

	$username = @_[0];

	$uid = &local_user_exist($username);
	if($uid < 0)
	{
	        $error_msg = $text{'error_msg_user_not_exist_local'};
		return $error_msg;
	}

	&delete_user($username);
	return "Success";
}

1;
